package com.hz.ruoyiequipment.warning.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import com.hz.ruoyiequipment.warning.configuration.AliyunSmsProperties;
import darabonba.core.client.ClientOverrideConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
public class SendSmsUtil {
    private static final Logger logger = LoggerFactory.getLogger(SendSmsUtil.class);

    private final AliyunSmsProperties aliyunSmsProperties;

    @Autowired
    public SendSmsUtil(AliyunSmsProperties aliyunSmsProperties) {
        this.aliyunSmsProperties = aliyunSmsProperties;
    }

    /**
     * 发送短信的工具方法
     * @param signName 短信签名名称
     * @param templateCode 短信模板 CODE
     * @param phoneNumbers 接收短信的手机号码
     * @param templateParam 短信模板变量对应的实际值
     * @return 短信发送响应结果的 JSON 字符串
     * @throws RuntimeException 可能抛出的运行时异常
     */
    public String sendSms(String signName, String templateCode, String phoneNumbers, String templateParam) {
        try {
            // Configure Credentials authentication information, including ak, secret, token
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId(aliyunSmsProperties.getAccessKeyId())
                    .accessKeySecret(aliyunSmsProperties.getAccessKeySecret())
                    .build());

            // Configure the Client
            AsyncClient client = AsyncClient.builder()
                    .region(aliyunSmsProperties.getRegionId())
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    .setEndpointOverride(aliyunSmsProperties.getEndpoint())
                    )
                    .build();

            // Parameter settings for API request
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName(signName)
                    .templateCode(templateCode)
                    .phoneNumbers(phoneNumbers)
                    .templateParam(templateParam)
                    .build();

            // Asynchronously get the return value of the API request
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            // Synchronously get the return value of the API request
            SendSmsResponse resp = response.get();
            String result = new Gson().toJson(resp);

            // Finally, close the client
            client.close();

            return result;
        } catch (Exception e) {
            logger.error("短信发送失败", e);
            throw new RuntimeException("短信发送失败", e);
        }
    }
}